@extends('backend.layouts.base')
@section('title', '角色管理')
@section('content')
    <link rel="stylesheet" href="/static/css/backend/detail.css">
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-crumbs">
                <a @click="goParent()">角色管理</a> > 编辑角色
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">角色信息</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="角色" prop="name">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="是否启用"  >
                            <template>
                                <el-switch
                                        v-model="form.status"
                                        :active-value="1"
                                        :inactive-value="0"
                                >
                                </el-switch>
                            </template>
                            <div>提示：如果禁用，则当前角色的操作员全部会禁止使用</div>
                        </el-form-item>
                    </div>
                </div>
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">角色权限</div>
                    </div>
                    <div class="vue-main-form">
                        <el-checkbox v-model="ischeckedAll"  style="margin-left:30px;" @change="checkedAll"><b>一键勾选</b></el-checkbox>
                        <el-collapse v-model="active" accordion>
                            <template v-for="(item,index) in permissions" class="warp">
                                <el-collapse-item  :name="index">
                                    <template slot="title">
                                        <el-checkbox v-model="item.checked" @change="firstUpdate(item)" style="margin-left:30px;"><b>[[item.name]]</b></el-checkbox>
                                    </template>
                                    <div v-for="(list,index) in item.child" style="font-size:0;display:flex;border-bottom:1px solid #EBEEF5;padding:10px 0;">
                                        <div style="width:20%;"><el-checkbox v-model="list.checked" @change="secondUpdate(item,list)" style="width:200px;margin-left:45px;"><b>[[list.name]]</b></el-checkbox></div>
                                        <div style="flex:1;display:flex;flex-wrap:wrap;">
                                            <div v-for="(obj,index) in list.child" style="width:200px;display:inline-block;">
                                                <el-checkbox v-model="obj.checked" @change="thirdUpdate(item,list,obj)">[[obj.name]]</el-checkbox>
                                            </div>
                                        </div>
                                    </div>
                                </el-collapse-item>
                            </template>
                        </el-collapse>
                    </div>
                </div>
            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
        </div>
    </div>
    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'test',
            data() {
                return{
                    id:'',
                    role:'',
                    userPermissions:[],
                    active:'0',
                    activeName: 'first',
                    permissions:[],
                    form:{
                        name:'',
                        status:''
                    },
                    arr:[],
                    ischeckedAll:false,//是否全选
                    rules:{
                        name:{ required: true, message: '请输入角色名称'}
                    },
                }
            },
            created() {

            },
            mounted() {
                this.id = getParam("id");
                if(this.id) {
                    this.is_edit = true;
                    this.submit_url = `{!! absolute_admin_url('sys.role.update') !!}`;
                }
                else {
                    this.submit_url = `{!! absolute_admin_url('sys.role.create') !!}`;
                }
                this.getData();
            },
            methods: {
                goParent() {
                    window.location.href = `{!! absolute_admin_url('sys.role.index') !!}`;
                },
                checkedAll(){
                    this.permissions.forEach(item=>{
                        item.checked = this.ischeckedAll;
                        this.firstUpdate(item)
                    })
                },
                getData() {
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('sys.role.getDetail') !!}',{id:this.id}).then(function (response) {
                            if (response.data.result){
                                this.role = response.data.data.role;
                                this.userPermissions = response.data.data.userPermissions;
                                this.permissions = response.data.data.permissions;
                                this.form.name = this.role.name;
                                this.form.status = this.role.status;
                                this.getChecked();
                            } else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                getChecked(){
                    this.permissions.forEach((item,index) => {
                        if(this.userPermissions.includes(item.key_name)){
                            item.checked=true
                        }else{
                            item.checked=false
                        }
                        if(item.child&&item.child.length>0){
                            item.child.forEach((list,index)=>{
                                if(this.userPermissions.includes(list.key_name)){
                                    list.checked=true
                                }else{
                                    list.checked=false
                                }
                                if(list.child&&list.child.length>0){
                                    list.child.forEach((obj,index)=>{
                                        if(this.userPermissions.includes(obj.key_name)){
                                            obj.checked=true
                                        }else{
                                            obj.checked=false
                                        }
                                    })
                                }
                            })
                        }
                    });
                    this.$forceUpdate()
                },
                firstUpdate(item){
                    if(item.checked){
                        if(item.child&&item.child.length>0){
                            item.child.forEach((list,index)=>{
                                list.checked=true
                                if(list.child&&list.child.length>0){
                                    list.child.forEach((obj,index)=>{
                                        obj.checked=true
                                    })
                                }
                            })
                        }
                    }else if(!item.checked){
                        if(item.child&&item.child.length>0){
                            item.child.forEach((list,index)=>{
                                list.checked=false
                                if(list.child&&list.child.length>0){
                                    list.child.forEach((obj,index)=>{
                                        obj.checked=false
                                    })
                                }
                            })
                        }
                    }
                },
                secondUpdate(item,list){
                    if(list.checked){
                        if(list.child&&list.child.length>0){
                            list.child.forEach((obj,index)=>{
                                obj.checked=true
                            })
                        }
                    }else if(!list.checked){
                        if(list.child&&list.child.length>0){
                            list.child.forEach((obj,index)=>{
                                obj.checked=false
                            })
                        }
                    }
                    let a=item.child.some((block,index)=>{
                        return block.checked==true
                    })
                    if(a){
                        item.checked=true
                    }else{
                        item.checked=false
                    }
                    this.$forceUpdate()
                },
                thirdUpdate(item,list,obj){
                    if(list.child.length>0){
                        let a=list.child.some((obj,index)=>{
                            return obj.checked==true
                        })

                        if(a){
                            list.checked=true
                        }else{
                            list.checked=false
                        }
                    }
                    if(item.child.length>0){
                        let b=item.child.some((list,index)=>{
                            return list.checked==true
                        })

                        if(b){
                            item.checked=true
                        }else{
                            item.checked=false
                        }
                    }
                    this.$forceUpdate()
                },
                submitForm(formName) {
                    this.arr=[];
                    this.permissions.forEach((item,index) => {
                        if(item.checked){
                            this.arr.push(item.key_name)
                            if(item.child&&item.child.length>0){
                                item.child.forEach((list,index)=>{
                                    if(list.checked){
                                        this.arr.push(list.key_name)
                                        if(list.child&&list.child.length>0){
                                            list.child.forEach((obj,index)=>{
                                                if(obj.checked){
                                                    this.arr.push(obj.key_name)
                                                }
                                            })
                                        }
                                    }
                                })
                            }
                        }
                    });
                    if(this.id) {
                        this.form.id = this.id
                    }
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post(this.submit_url,{role:this.form,permission:this.arr}).then(response => {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.goBack();
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
            },
        })

    </script>
@endsection
